using System;
using System.Collections.Generic;
using System.Data;
using VIC.User.Models;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.ContentManagement;
using Orchard;

namespace VIC.User.DataMigrations
{
    public class Migrations : DataMigrationImpl
    { 
        private readonly IOrchardServices _orchardServices;
        public Migrations(IOrchardServices orchardServices)
        {
           _orchardServices = orchardServices;
        }
       
        
        public int Create()
        {
            // Creating table MapRecord
            SchemaBuilder.CreateTable("VIC_UserRecord", table => table
                .ContentPartRecord()
                .Column("FirstName",DbType.String)
                .Column("LastName", DbType.String)
                .Column("Gender", DbType.String)
                .Column("DateOfBirth", DbType.String)
                .Column("Email", DbType.String)
                .Column("Address", DbType.String)
                .Column("Phonenumber", DbType.String)
                .Column("SummaryCare", DbType.String)
                .Column("SummarySelf", DbType.String)
                .Column("Website", DbType.String)
                .Column("SSN",DbType.String)
                .Column("Avatar",DbType.String)
                .Column<int>("UserRecord_Id")
            );

            ContentDefinitionManager.AlterPartDefinition(
                typeof(VIC_UserPart).Name, cfg => cfg.Attachable());

            return 1;
        }
        public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterTypeDefinition("VIC_User", cfg =>
                {
                    cfg.WithPart("VIC_UserPart");
                    cfg.WithPart("CommonPart");
                    cfg.Creatable(true);
                    cfg.Draftable(false);
                });
            return 2;
        }
        public int UpdateFrom2()
        {
            var settings = _orchardServices.WorkContext.CurrentSite.As<Orchard.Users.Models.RegistrationSettingsPart>();
            settings.UsersCanRegister = true;
            settings.UsersMustValidateEmail = true;
            settings.EnableLostPassword = true;
            IOrchardServices services = null;
            if (_orchardServices.WorkContext.TryResolve<IOrchardServices>(out services) == true)
            {
                services.ContentManager.UpdateEditor(settings, null);
            }
            return 3;
        }
    }
}